﻿/* Copyright 2008-10 Jarrett Vance
 * see http://www.codeplex.com/blogsvc/license */
namespace AtomSite.WebCore
{
  using System.Web.Mvc;
  using System.Web.Routing;
  using StructureMap;
  using StructureMap.Attributes;
  using System.Collections.Generic;
  using AtomSite.Domain;

  public class BlogPlugin : BasePlugin
  {
    public BlogPlugin(ILogService logger)
      : base(logger)
    {
      DefaultMerit = (int)MeritLevel.High + 20;
    }
    public override void Register(IContainer container, List<SiteRoute> routes, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders, ICollection<Asset> globalAssets)
    {
      container.Configure(c =>
      {
        c.ForRequestedType<IBlogService>().CacheBy(InstanceScope.Singleton)
          .TheDefault.Is.OfConcreteType<BlogService>();
        c.ForRequestedType<ITrackbackService>().CacheBy(InstanceScope.Singleton)
          .TheDefault.Is.OfConcreteType<TrackbackService>();
        c.ForRequestedType<ICleanContentService>().TheDefault.Is.OfConcreteType<XhtmlCleanContentService>();
      });

      RegisterController<BlogController>(container);
      RegisterController<TrackbackController>(container);

      RegisterPage(container, "BlogEntry", new[] { "Blog.js" });

      RegisterWidget<BlogCommentsWidget>(container);
      RegisterWidget<BlogAddCommentWidget>(container);
      RegisterCompositeWidget(container, "BlogTrackbackWidget", "Trackback", "TrackbackWidget");
      RegisterCompositeWidget(container, "BlogArchiveWidget", "Widget", "FullFeed");
      RegisterCompositeWidget(container, "BlogCategoriesWidget", "Widget", "FullFeed");
      RegisterCompositeWidget(container, "BlogCategoryCloudWidget", "Widget", "FullFeed");
      RegisterCompositeWidget(container, "BlogRecentWidget", "Widget", "SizeFeed");
      RegisterCompositeWidget(container, "BlogSummaryWidget", "Widget", "SizeFeed");
      RegisterCompositeWidget(container, "BlogRecentCommentsWidget", "Widget", "SizeAnnotations");
      RegisterCompositeWidget(container, "BlogEntrySimpleWidget", "Widget", "Entry");
      RegisterCompositeWidget(container, "BlogSettingsWidget", "Blog", "BlogSettings");
      RegisterWidget<BlogSearchWidget>(container);

      RegisterRoutes<TrackbackRouteRegistrar>(container, routes);
      RegisterRoutes<BlogRouteRegistrar>(container, routes);

    }

    public override PluginState Setup(IContainer container, string appPath)
    {
      SetupBlogSettings(container);
      return base.Setup(container, appPath);
    }

    public override PluginState Upgrade(IContainer container, string appPath, System.Version previous, System.Version current)
    {
      if (previous == ServerApp.Version11) SetupBlogSettings(container);
      return base.Upgrade(container, appPath, previous, current);
    }

    void SetupBlogSettings(IContainer container)
    {
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsTabs",
        new LiteralInclude("<li><a href=\"#blogSettings\">Blog Settings</a></li>"));
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsPanes", "BlogSettingsWidget");
    }
  }
}